<?php

namespace App\Http\Controllers;

use App\Entities\Course;
use App\Repositories\CourseRepository;
use Illuminate\Http\Request;

class CourseController extends Controller
{
    private $course;
    public function __construct(CourseRepository $course)
    {
        $this->course = $course;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $list = $this->course->all();
        return response()->json([
            'errors' => false,
            'count' => $list->count(),
            'data' => $list
        ]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $course = $this->course->create($request->all());
        return response()->json([
            'errors' => false,
            'message' => '新建成功',
            'data' => $course
        ]);
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Entities\Course  $course
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $course = $this->course->find($id);
        return response()->json([
            'errrors' => false,
            'data' => $course
        ]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Entities\Course  $course
     * @return \Illuminate\Http\Response
     */
    public function edit(Course $course)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Entities\Course  $course
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $course = $this->course->find($id);
        $newCourse = $this->course->update($course, $request->all());
        return response()->json([
            'errors'=> false,
            'data' => $newCourse
        ]);
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Entities\Course  $course
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $course = $this->course->find($id);
        $ret = $this->course->destroy($course);
        return response()->json([
            'errors' => false,
            'data' => $ret
        ]);
        //
    }
}
